import Vue from 'vue';
import { Document } from './document';
import { Workbench } from './workbench';
import { Viewport } from './viewport';
import { Dragon } from './dragon';
import { EngineProps, ISetters, IAssets } from '../shared/types'

export class Engine {
  document: Document;
  workbench: Workbench;
  viewport: Viewport;
  dragon: Dragon;

  constructor({ assets, setters }: EngineProps) {
    this.document = new Document(this);
    this.workbench = new Workbench(this);
    this.viewport = new Viewport(this);
    this.dragon = new Dragon(this);

    this.register(assets, setters);
    this.initDocument();
  }

  initDocument() {
    this.document.init()
  }

  register(assets: IAssets, setters: ISetters){
    this.registerAssets(assets);
    this.registerSetters(setters);
  }

  registerAssets(assets: IAssets) {
    if(!assets) return;
    const { components } = assets;

    Object.keys(components).forEach(name => {
      const component = components[name];
      Vue.component(component.componentName, component.component)
    })

    this.workbench.updateAssets(assets);
  }

  registerSetters(setters: ISetters) {
    Object.keys(setters).forEach(name => {
      Vue.component(name, setters[name])
    })

    this.workbench.updateSetters(setters);
  }
}
